package com.handydata.antifraud.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;


@Slf4j
public class HttpUtil {

  private static SslContextUtils sslContextUtils = new SslContextUtils();

    public static String sendDataByPost(String postUrl, String xmlData) throws Exception{

      return  sendDataByPost(postUrl, xmlData, 5000);

    }
    public  static String CONNECT_TIMED_OUT="connect timed out";
    public  static String READ_TIMED_OUT="Read timed out";


    public static String sendDataByPost(String postUrl, String xmlData,int timeOut) throws Exception{

            return  sendDataByPost(postUrl,xmlData,timeOut,"text/xml");
    }
    public static String sendDataByPost(String postUrl, String data,int timeOut,String contentType) throws Exception{
    StringBuilder sb = new StringBuilder();
    try {
        URL url = new URL(postUrl);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

        if(httpConn instanceof HttpsURLConnection){
          sslContextUtils.initHttpsConnect((HttpsURLConnection)httpConn);
        }

        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        if(contentType!=null)
        httpConn.setRequestProperty("Content-type", contentType);
        httpConn.setConnectTimeout(2000);
        httpConn.setReadTimeout(timeOut);
        // 发送请求
        httpConn.getOutputStream().write(data.getBytes("utf-8"));
        httpConn.getOutputStream().flush();
        httpConn.getOutputStream().close();
        // 获取输入流
        BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"utf-8"));
        char[] buf = new char[1024];
        int length = 0;
        while ((length = reader.read(buf)) > 0) {
          sb.append(buf, 0, length);
        }
        reader.close();
      } catch (MalformedURLException e) {
        log.error("url 参数不对 {}",e.getMessage());
      }catch (SocketTimeoutException e){
        log.error(" http 请求超时 {}",e.getMessage());
        if(CONNECT_TIMED_OUT.equals(e.getMessage())){
            return CONNECT_TIMED_OUT;
        }

      }
      catch (IOException e) {
        log.error(" 网络有错误 {} {}" ,postUrl,e.getMessage());
      }
      catch (Exception e) {
        log.error("http请求出错 {}",e.getMessage());
      }
      return sb.toString();
  }

    public static InputStream getFileStream(String postUrl, int timeOut) throws Exception{
        StringBuilder sb = new StringBuilder();
        try {
            URL url = new URL(postUrl);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

            if(httpConn instanceof HttpsURLConnection){
                sslContextUtils.initHttpsConnect((HttpsURLConnection)httpConn);
            }

            httpConn.setRequestMethod("GET");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            //httpConn.setRequestProperty("Content-type", contentType);
            httpConn.setConnectTimeout(2000);
            httpConn.setReadTimeout(timeOut);
            // 发送请求
            //httpConn.getOutputStream().write(xmlData.getBytes("utf-8"));
            //httpConn.getOutputStream().flush();
            //httpConn.getOutputStream().close();
            // 获取输入流
           return httpConn.getInputStream();
        } catch (MalformedURLException e) {
            log.error("url 参数不对 {}",e.getMessage());
        }catch (SocketTimeoutException e){
            log.error(" http 请求超时 {}",e.getMessage());
            if(CONNECT_TIMED_OUT.equals(e.getMessage())){
                return null;
            }
        }
        catch (IOException e) {
            log.error(" 网络有错误 {} {}" ,postUrl,e.getMessage());
        }
        catch (Exception e) {
            log.error("http请求出错 {}",e.getMessage());
        }
        return null;
    }
  
}
